package com.itheima.controller;

import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author liuyp
 * @since 2025/7/19
 */
@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {

    @Resource
    private DeptService deptService;

    @GetMapping
    public Result queryAll(){
        log.info("查询所有部门");

        // 调用service完成功能，得到结果数据
        List<Dept> deptList = deptService.list();
        // 将结果数据封装成Result对象并返回
        return Result.success(deptList);
    }

    @DeleteMapping
    public Result deleteById(Integer id){
        log.info("根据id删除部门，id={}", id);
        deptService.removeById(id);
        return Result.success();
    }

    @PostMapping
    public Result add(@RequestBody Dept dept){
        log.info("新增部门，dept={}", dept);
        deptService.save(dept);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        log.info("根据id查询部门，id={}", id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }

    @PutMapping
    public Result update(@RequestBody Dept dept){
        log.info("修改部门，dept={}", dept);
        deptService.updateById(dept);
        return Result.success();
    }
}
